<template>
  <div class="app-container">
    <div>
      <!-- 条件栏 -->
      <PageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      >
        <template v-slot:filter-time>
          <el-date-picker
            @change="timeChange"
            v-model="time"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            align="right"
            :shortcuts="$fn.pickerOptionsFn()"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </template>
      </PageFilter>

      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        :pageTotal="pageInfos.pageTotal"
        v-model:data="tableInfo.data"
        :tab-index="true"
        :check-box="false"
        :summary="true"
        :summaryByPlatform="true"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :api="tableInfo.api"
      >
        <template v-slot:col-sumRate="scope">
          <p>{{ scope.row.sumRate + '%' }}</p>
        </template>
        <template v-slot:col-userSumRate="scope">
          <p>{{ scope.row.userSumRate + '%' }}</p>
        </template>
      </TableContainer>
    </div>
  </div>
</template>

<script setup name="productUseRateQuery">
import moment from 'moment'

import {
  productUseRateQueryPage,
  productUseRateQueryExcel,
  productUseRateQueryPrintByDepot,
} from '@/api/dataQuery.js'
import { fatchTemplateByCode } from '@/api/base'
const { proxy } = getCurrentInstance()
const time = ref([])
const defaultTime = ref([])
const filterInfo = ref({
  query: {
    deptIdMult: '',
    planTime: '',
    planType: '',
    startTime: '',
    endTime: '',
  },
  list: [
    {
      type: 'input',
      label: '名称/别名/编码/助记符',
      value: 'keyword',
      event: search,
    },
    { label: '日期', value: 'time', type: 'slot', event: search },
    { type: 'button', label: '搜索', event: search },
    {
      type: 'button',
      label: '打印',
      event: print,
      show: proxy.$permission('productUseRateQuery:print'),
    },
    {
      type: 'button',
      label: '导出excel',
      event: exportExcel,
      show: true,
    },
  ],
})
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: null,
  selections: [],
  fieldList: [
    { label: '试剂编码', value: 'productCode', type: 'column' },
    {
      label: '试剂名称',
      value: 'commonName',
      minWidth: 220,
      type: 'column',
    },
    {
      label: '期间出库数',
      value: 'exportQty',
      type: 'column',
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      label: '单个测试数',
      value: 'testNumber',
      type: 'column',
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      label: '理论测试数',
      value: 'testSumNumber',
      type: 'column',
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      label: '实际测试总数',
      value: 'actualSumNumber',
      type: 'column',
      formatter: proxy.$fn.qtyFormatter,
    },
    {
      label: '实际测试人数',
      value: 'actualUserSumNumber',
      type: 'column',
      formatter: proxy.$fn.qtyFormatter,
    },
    { label: '总数占比', value: 'sumRate', type: 'slot' },
    { label: '测试人数占比', value: 'userSumRate', type: 'slot' },
    {
      label: '出库总金额',
      value: 'exportSumAmount',
      type: 'column',
      formatter: proxy.$fn.qtyFormatter,
    },
  ],
})
const pageInfos = ref({
  pageSize: 20,
  curPage: 1,
  pageTotal: 0,
})
watch(
  () => filterInfo.value.query.createTime_from,
  (val) => {
    if (!val) {
      time.value = null
    }
  }
)
function exportExcel() {
  productUseRateQueryExcel(
    proxy.$fn.delEmptyAttribute(filterInfo.value.query)
  ).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function print() {
  fatchTemplateByCode({
    resourceCode: 'productUseRateQuery:print',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      let params = {
        ...filterInfo.value.query,
      }
      productUseRateQueryPrintByDepot(params).then((resp) => {
        if (resp && resp.success) {
          let data = proxy.$fn.deepClone(resp.data)
          let newData = data.map((item) => {
            return {
              ...item,
              ...item.orderInfo,
            }
          })
          proxy.$lodop.preview(temp, newData)
        }
      })
    }
  })
}
function timeChange(val) {
  if (val && val.length) {
    filterInfo.value.query.startTime = val[0]
    filterInfo.value.query.endTime = val[1]
  } else {
    filterInfo.value.query.startTime = ''
    filterInfo.value.query.endTime = ''
  }
}
function getList() {
  tableInfo.value.api = productUseRateQueryPage
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function getCurrentMonthFirst() {
  var date = new Date()
  date.setDate(1)
  console.log(new Date(date.setDate(1)), new Date())
  var month = parseInt(date.getMonth() + 1)
  var day = date.getDate()
  if (month < 10) month = '0' + month
  if (day < 10) day = '0' + day
  filterInfo.value.query.startTime =
    date.getFullYear() + '-' + month + '-' + day + ' 00:00:00'
}
function getCurrentMonthLast() {
  filterInfo.value.query.endTime = moment(new Date()).format(
    'YYYY-MM-DD HH:mm:ss'
  )
  const startDate = new Date()
  startDate.setDate(1)
  startDate.setHours(0)
  startDate.setMinutes(0)
  startDate.setSeconds(0)
  time.value = [
    moment(new Date(startDate)).format('YYYY-MM-DD HH:mm:ss'),
    moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
  ]
}
onMounted(() => {
  getCurrentMonthFirst()
  getCurrentMonthLast()
  getList()
})
</script>

<style scoped></style>
